import Vue from 'vue'
import Vuex from 'vuex'
import { getComments, postComment } from '../../services/CommentRequest'

Vue.use(Vuex)

export default {
  state: {
    comments: []
  },
  getters: {
    getComments: (state) => {
      return state.comments
    }
  },
  mutations: {
    setComments(state, comments) {
      state.comments = comments
    },
    addComment(state, comment) {
      //console.log('hello', comment)
      state.comments.unshift(comment)
      console.log(state.comments)
    }
  },
  actions: {
    setComments({ commit }, data) {
      return new Promise((resolve, reject) => {
        getComments(data)
          .then((comments) => {
            console.log(comments.data.data.list)
            commit('setComments', comments.data.data.list)
            resolve(comments)
          })
          .catch((err) => reject(err))
      })
    },
    addComment({ commit }, comment) {
      return new Promise((resolve, reject) => {
        postComment(comment)
          .then((res) => {
            console.log(res.data.data)
            commit('addComment', res.data.data)
            resolve(res)
          })
          .catch((err) => reject(err))
      })
    }
  }
}
